package com.lzw.bluetoothpractice.Activitys;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.lzw.bluetoothpractice.Fragment.MessageFragment;
import com.lzw.bluetoothpractice.Fragment.MyFragment;
import com.lzw.bluetoothpractice.Fragment.PlanFragment;
import com.lzw.bluetoothpractice.Fragment.ShouyeFragment;
import com.lzw.bluetoothpractice.R;

public class IndexActivity extends AppCompatActivity implements View.OnClickListener {

    //定义主界面下的四个按钮
    private Button bt_shouye,bt_plan,bt_message,bt_my;
    //初始化四个碎片Fragment
    ShouyeFragment shouye;
    PlanFragment plan = new PlanFragment();
    MessageFragment message;
    MyFragment my;

    Fragment fragments[];
    Button buttons[];    //存放四个按钮
    int index;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      /*  // 隐藏标题栏
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }*/
        setContentView(R.layout.activity_index);
        init();
        // 注册事件监听器
        buttons[0].setOnClickListener(this);
        buttons[1].setOnClickListener(this);
        buttons[2].setOnClickListener(this);
        buttons[3].setOnClickListener(this);
    }
    //进行初始化
    private void init() {
        //对控件的初始化
        bt_shouye = (Button)this.findViewById(R.id.button_shouye);
        bt_plan = (Button)this.findViewById(R.id.button_plan);
        bt_message = (Button)this.findViewById(R.id.button_message);
        bt_my = (Button)this.findViewById(R.id.button_my);
        shouye = new ShouyeFragment();
        plan = new PlanFragment();
        message = new MessageFragment();
        my = new MyFragment();
        fragments = new Fragment[]{shouye,plan,message,my};
        buttons = new Button[]{bt_shouye,bt_plan,bt_message,bt_my};
        //设置默认fragment
        int id = getIntent().getIntExtra("id", 0);
        if(id!=0){
            setFragment(id,3);
        }else {
            setFragment(0,1);
        }


    }
    //设置默认的Fragment
    private void setFragment(int currentIndex, int index) {
        if (currentIndex != index) {
            //在androidx支持的包中
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.fl,fragments[currentIndex]);
            ft.commit();
            //直接创建FragmentTransaction对象即可，然后调用对象中的方法进行Fragment页面布局的加载
            buttons[currentIndex].setSelected(true);
            buttons[index].setSelected(false);
        }
    }
    //重写按钮监听的方法
    @Override
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.button_shouye:
                setFragment(0, index);
                index = 0;
                break;
            case R.id.button_plan:
                setFragment(1, index);
                index = 1;
                break;
            case R.id.button_message:
                setFragment(2, index);
                index = 2;
                break;
            case R.id.button_my:
                setFragment(3,index);
                index = 3;
                break;
        }
    }

    @Override
    public void startActivityFromFragment(@NonNull Fragment fragment, Intent intent, int requestCode) {
        super.startActivityFromFragment(fragment, intent, requestCode);

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
